package com.cooba.activitys.user;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cooba.R;
import com.cooba.utils.Md5Utils;
import com.cooba.utils.MySqlOpenHelper;
import com.cooba.utils.ServerURL;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
/**
 * Created by Administrator on 2015/12/16.
 * 修改密码
 */
public class ChangPasswordActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText mEt_originalPassword;
    private EditText mNew_Password;
    private EditText mAgainPassword;
    private Button mSndButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_number);
        mEt_originalPassword = (EditText) findViewById(R.id.et_originalPassword);
        mNew_Password = (EditText) findViewById(R.id.new_Password);
        mAgainPassword = (EditText) findViewById(R.id.againPassword);
        mSndButton = (Button) findViewById(R.id.sendButton);
//        mEt_originalPassword.setOnClickListener(this);
//        mNew_Password.setOnClickListener(this);
//        mAgainPassword.setOnClickListener(this);
        mSndButton.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.sendButton:


                if (TextUtils.isEmpty(mEt_originalPassword.getText()) || TextUtils.isEmpty(mNew_Password.getText()) || TextUtils.isEmpty(mAgainPassword.getText())) {
                } else {

                    String originalPassword = Md5Utils.encodeBy32BitMD5(mEt_originalPassword.getText().toString());
                    String New_Password = Md5Utils.encodeBy32BitMD5(mNew_Password.getText().toString());
                    String AgainPassword = Md5Utils.encodeBy32BitMD5(mAgainPassword.getText().toString());
                    if (New_Password.equals(AgainPassword)) {
                        MySqlOpenHelper helper = new MySqlOpenHelper(getApplication());
                        SQLiteDatabase db = helper.getReadableDatabase();
                        Cursor cursor = db.query("login_date", null, null, null, null, null, null, null);


                        String login_id=null;
                        if (cursor.moveToFirst()) {
                            login_id = cursor.getString(cursor.getColumnIndex("login_id"));
                            Log.e("login_id", "login_id"+login_id);
                        }
                        Log.e("login_id", "login_id2"+login_id);
                        HttpUtils httpUtils = new HttpUtils();
                        RequestParams params = new RequestParams();
                        params.addBodyParameter("login_id", login_id);
                        params.addBodyParameter("old_password", originalPassword);
                        params.addBodyParameter("new_password", New_Password);
                        httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.ChangePwd, params, new RequestCallBack<String>() {
                            @Override
                            public void onFailure(HttpException arg0, String arg1) {
                                Log.e("sss", "获取验证码失败");
                            }

                            @Override
                            public void onSuccess(ResponseInfo<String> responseInfo) {
                                String msg = JSON.parseObject(responseInfo.result).getString("msg");
                                String status = JSON.parseObject(responseInfo.result).getString("status");
                                Log.e("ssssss", msg + ">>status=" + status);
                                if (status.equals("0")) {
                                    Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT).show();
                                } else {

                                }
                            }
                        });
                        cursor.close();
                        db.close();
                    } else {
                        Toast.makeText(getApplication(), "两次输入密码不一样", Toast.LENGTH_SHORT).show();
                    }

                }
                break;

        }
    }
}
